#ifndef _CDATASERVER_H_
#define _CDATASERVER_H_

class CDataServer : public dbus::CObject
{
public:
    CDataServer();
    ~CDataServer();
    
    struct SMetadata
    {
        TID mID;
        std::string mName;
        int mType;
        bool mMultiple;
        int mWeight;
    };

protected:
////////////////////////
// database functions
//
    void OpenDatabase();
    void CloseDatabase();
    void PrepareQueries();

////////////////////////
// data functions
//
    //initialize
    void ReadMetadata();

    //read
    SMetadata *AddMetadata(const std::string &pName,int pType,
                        bool pMultiple,int pWeight);
    SMetadata *GetMetadataType(const std::string &pMetadata,bool pCreate);
    TID GetFolderID(const std::string &pFolder,bool pCreate);
    TID GetObjectID(TID pFolderID,const std::string &pName,bool pCreate);
    std::string GetObjectMime(TID pFolderID,const std::string &pName);

    TID InsertObject(TID pFolderID,const std::string &pName,const std::string &pMIME,
                    int pFileSize,bool pIsFolder,bool pIsLink,int pTime);
    void GetMimeTypes(const std::string &pFolder,dbus::CReply *pReply);

    void GetMetadata(const std::string &pFolder,const std::string &pName,
                    const std::string &pKey,UValue &pValue);
    void SetMetadata(const std::string &pFolder,const std::string &pName,
                  const std::string &pKey,const UValue &pValue);

//dbus functions
    virtual void DBusCall(dbus::CMessage*);

//database data
    CDatabaseEngine *mDBEngine;
    CDatabase *mDB;

    struct
    {
        CDBQuery *mInsertMetadata;
        CDBQuery *mInsertTag;
        CDBQuery *mInsertFolder;
        CDBQuery *mInsertObject;
        CDBQuery *mInsertObjectTag;
        CDBQuery *mInsertObjectMetadata;
        CDBQuery *mGetFolderID;
        CDBQuery *mGetObjectID;
        CDBQuery *mGetObject;
        CDBQuery *mGetMetadata;
        CDBQuery *mGetObjectMetadata;
    } mQueries;

    struct
    {
        std::unordered_map<TID,SMetadata*> mMetadataByID;
        std::unordered_map<std::string,SMetadata*> mMetadataByName;
    } mCache;
};

#endif
